<?php
Junction_FileCabinet::using("Junction_Libs_Catalog");
Junction_FileCabinet::using("Test_Database_Catalog");
Junction_FileCabinet::using("Junction_Db_Pdo_Service");

class Test_Junction_Db_Pdo_PdoServiceTest extends UnitTestCase 
{
	
	private $_validDsn;
	private $_invalidDsn;
	
	private $_query = "INSERT INTO CustomAttributes (`key`, `value`) VALUES ('foo', 'bar')";
	private $_query1 = "UPDATE CustomAttributes SET value = 'baz'";
	private $_query2 = "UPDATE CustomAttributes SET value = 'baz' WHERE 1 = 0";
	
	public function __construct()
	{
		$this->_validDsn = "sqlite:". Test_Database_Catalog::fetch("test.db");
		$this->_invalidDsn = "sqlite:FOO";
	}
	
	public function testConstructor()
	{
		try{
			$dbConn = new Junction_Db_Pdo_Service($this->_validDsn);
		}catch(Junction_Db_Common_SQlException $e){
			$this->fail();
		}
		$this->pass();
		
		try{
			$dbConn = new Junction_Db_Pdo_Service($this->_invalidDsn);
		}catch(Junction_Db_Common_SqlException $e){
			$this->pass();
			return;
		}
		$this->fail();		
	}
	
	public function testLastInsertId()
	{
		try{
			$dbConn = new Junction_Db_Pdo_Service($this->_validDsn);
			$this->assertTrue($dbConn->save($this->_query));
		}catch(Junction_Db_Common_SqlException $e){
			$this->fail($e->getMessage());
		}
		
		$this->assertTrue($dbConn->lastInsertId() > 0);
	}
	
	public function testAffectedRows()
	{
		try{
			$dbConn = new Junction_Db_Pdo_Service($this->_validDsn);
			$this->assertTrue($dbConn->save($this->_query1));
			$this->assertTrue($dbConn->affectedRows() > 0);
			
			$this->assertTrue($dbConn->save($this->_query2));
			$this->assertEqual($dbConn->affectedRows(), 0);
		
		}catch(Junction_Db_Common_SqlException $e){
			$this->fail($e->getMessage());
		}
	}
	
	public function testPrepare()
	{
		try{
			$dbConn = new Junction_Db_Pdo_Service($this->_validDsn);
			$this->assertTrue(is_a($dbConn->prepare($this->_query1), 'Junction_Db_Pdo_PreparedStatement'));
		}catch(Db_Common_SqlException $e){
			$this->fail($e->getMessage());
		}		
	}
	
	public function testTransaction()
	{
		
	}
}
?>